<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2020-2030 衡水山木枝技术服务有限公司
 * @Date: 2021-06-10 20:18:29
 * @Description: 供应商员工管理
 * @LastEditors: 李志刚
 * @LastEditTime: 2021-09-17 15:51:23
 * @FilePath: /qingshanERP/app/Http/Controllers/Console/QingShanERP/SupplierStaffController.php
 */

namespace App\Http\Controllers\Console\QingShanERP;

use Validator;
use App\Customize\Func;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\QingShanERP\SupplierStaff;
use App\Models\Tenant\SupplierContactTenant;
use App\Models\Tenant\SupplierTenant;
use App\Models\Tenant\Tenant;
use Illuminate\Support\Facades\DB;

class SupplierStaffController extends Controller
{
    public function getList(Request $req)
    {
        try {
            $supplier_id = $req->input('supplier_id', 0);
            $list = SupplierStaff::where('supplier_id',$supplier_id)->where('del_flag', 0)->orderBy('id', 'desc')->get();
            return $this->resData(200, '获取数据成功', $list);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请稍后再试...');
        }
    }

    public function postCreate(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'supplier_id' => 'required|integer',
                'staffname' => 'required|max:20',
                'phone' => 'required|regex:/^1[3456789]\d{9}$/',
                'email' => 'required|email',
                'password' => 'confirmed|min:6|max:15|alpha_dash',
            ]);
            $attrs = array(
                'supplier_id' => '供应商ID',
                'staffname' => '姓名',
                'phone' => '手机号',
                'email' => '邮箱',
                'password' => '密码',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $all = $request->all();
            $crypt = Func::str_random(10);
            $pwd = Func::makepwd($all['password'], $crypt);
            $create = ['supplier_id' => $all['supplier_id'], 'staffname' => $all['staffname'], 'phone' => $all['phone'], 'email' => $all['email'], 'status' => 1, 'password' => $pwd, 'crypt' => $crypt];
            SupplierStaff::create($create);
            return $this->resData(200, '添加成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '添加文章失败，请重新操作...');
        }
    }
    //详情
    public function postDetail(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'supplier_staff_id' => 'required',
            ]);
            $attrs = array(
                'supplier_staff_id' => '员工ID',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('supplier_staff_id');
            $info = SupplierStaff::find($id);
            return $this->resData(200, '获取数据成功...', $info);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请重新操作...');
        }
    }
    //编辑
    public function postEdit(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'supplier_staff_id' => 'required|integer',
                'staffname' => 'required|max:20',
                'phone' => 'required|regex:/^1[3456789]\d{9}$/',
                'email' => 'required|email',
                'password' => 'nullable|confirmed|min:6|max:15|alpha_dash',
            ]);
            $attrs = array(
                'supplier_staff_id' => '员工ID',
                'staffname' => '姓名',
                'phone' => '手机号',
                'email' => '邮箱',
                'password' => '密码',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('supplier_staff_id');
            $all = $request->all();
            $update = ['staffname' => $all['staffname'], 'phone' => $all['phone'], 'email' => $all['email'], 'status' => $all['status'] == 'true' ? 1 : 0];
            if (!is_null($all['password'])) {
                $crypt = Func::str_random(10);
                $pwd = Func::makepwd($all['password'], $crypt);
                $update['crypt'] = $crypt;
                $update['password'] = $pwd;
            }
            SupplierStaff::where('id', $id)->update($update);
            return $this->resData(200, '编辑成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '编辑失败，请重新操作...');
        }
    }

    //删除
    public function postRemove(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'supplier_staff_id' => 'required|array',
            ]);
            $attrs = array(
                'supplier_staff_id' => '员工ID',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('supplier_staff_id');
            SupplierStaff::whereIn('id', $id)->update(['del_flag' => 1]);
            return $this->resData(200, '删除成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '删除失败，请重新操作...');
        }
    }
    // 分配租户
    public function getTenant(Request $request){
        try {
            $validator = Validator::make($request->input(), [
                'supplier_staff_id' => 'required|integer',
            ]);
            $attrs = array(
                'supplier_staff_id' => '员工ID',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('supplier_staff_id');
            $supplier_id = SupplierStaff::where('id',$id)->value('supplier_id');
            $allIds = SupplierTenant::where('supplier_id',$supplier_id)->pluck('tenant_id');
            $all = Tenant::whereIn('id',$allIds)->get();
            $list = SupplierContactTenant::where('supplier_contact_id',$id)->pluck('tenant_id');
            return $this->resData(200, '获取数据成功...',['list'=>$list,'all'=>$all]);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请重新操作...');
        }
    }
    public function postTenant(Request $request){
        DB::beginTransaction();
        try {
            $validator = Validator::make($request->input(), [
                'supplier_staff_id' => 'required|integer',
                'tenant_id' => 'required|array',
            ]);
            $attrs = array(
                'supplier_staff_id' => '员工ID',
                'tenant_id' => '租户',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                DB::rollBack();
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('supplier_staff_id');
            SupplierContactTenant::where('supplier_contact_id',$id)->delete();
            $insert = [];
            foreach ($request->input('tenant_id',[]) as $t) {
                $insert[] = ['supplier_contact_id'=>$id,'tenant_id'=>$t];
            }
            SupplierContactTenant::insert($insert);
            DB::commit();
            return $this->resData(200, '更新数据成功...');
        } catch (\Throwable $e) {
            DB::rollBack();
            return $this->resData(500, '更新数据失败，请重新操作...');
        }
    }
}
